package com.txm.drawPic;

import java.io.*;
import java.util.Scanner;
import java.util.Vector;

/*
 * 用户记录游戏的一些数据：当前击杀敌方坦克
 * */
public class Record {
    private int killNum = 0;
    String filePath = "F://";
    String fileName = "myRecord.txt";

    Vector<String> tankPointInfo = new Vector<>();

    public boolean isNewGame = false;

    public int getKillNum() {
        return killNum;
    }

    public void setKillNum(int killNum) {
        this.killNum = killNum;
    }

    public void kill() {
        this.killNum++;
    }

    public Record() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入数字：新游戏-1，继续游戏-2");
            String i = scanner.next();
            if (i.equals("1")) {
                this.killNum = 0;
                this.isNewGame = true;
                break;
            } else if (i.equals("2")) {
                readData();
                this.isNewGame = false;
                break;
            }
        }
    }

    public void readData() {
        File file = new File(filePath, fileName);
        if (!file.exists()) {
            this.killNum = 0;
        } else {
            System.out.println("===============读取文件===============");
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new FileReader(file));

                String tankInfo = null;
                int index = 0;
                while ((tankInfo = bufferedReader.readLine()) != null){
                    if(index==0){
                        this.killNum = Integer.parseInt(tankInfo.split("=")[1]);
                    }else{
                        tankPointInfo.add(tankInfo);
                    }
                    index++;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    //  存储数据
    public void saveData() {
        File file = new File(filePath, fileName);
        if (!file.exists()) {
            try {
                if (file.createNewFile()) {
                    System.out.println(fileName + "创建成功");
                } else {
                    System.out.println(fileName + "创建失败");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            System.out.println("文件已存在");
        }
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));
            bufferedWriter.write("killNum=" + killNum);
            for (Tank tank : MyPanel.enemyTank) {
                bufferedWriter.newLine();
                bufferedWriter.write(tank.getX() + " " + tank.getY() + " " + tank.getDirect());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
